package helpers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import systems.ConfigSystem;

import entities.SiteConfig;

public class LanguageHelper {
	
	private static ResourceBundle translate;
	
	private static String language = "es";
	
	private static List<String> languages = new ArrayList<String>(Arrays.asList("en", "es", "pt"));
	
	static
	{
		SiteConfig siteConfig = ConfigSystem.getConfigs();
		language = siteConfig.getLanguage();
		
		translate = ResourceBundle.getBundle("language", new Locale(language));
	}
	
	public static void resetLanguage (SiteConfig siteConfig) {
		language = siteConfig.getLanguage();
		
		translate = ResourceBundle.getBundle("language", new Locale(language));
	}
	
	public static List<String> getLanguages() {
		return languages;
	}
	
	public static String getLanguage() {
		return language;
	}
	
	public static String getLanguage(String url) {
		String firstSubDomain = UrlHelper.getFirstSubDomain(url);
		if (languages.contains(firstSubDomain)) {
			language = firstSubDomain;
		}
		
		return language;
	}
	
	public static String getString(String key) {
		String value = "";
		try {
			value = translate.getString(key);
		} catch (Exception e) {}
		return value; 
	}
}
